/**
 * @author Francesco di Dio
 * Date: 20/apr/2013
 * Titolo: AccountGroupList.java
 * Versione: 
 */

/**
 * Copyright (c) 2013 Francesco di Dio.
 * tabuto83@gmail.com 
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */
package com.tabuto.jkeys.models;

import java.util.ArrayList;
import java.util.List;

public class AccountGroupList extends AccountGroup {

	/**
	 * 
	 */
	private static final long serialVersionUID = -4890534811294305052L;

	private List<AbstractJKeyModel> keyList = new ArrayList<AbstractJKeyModel>();

	public AccountGroupList() {
		super();
		setName("AccountGroup");
	}

	public AccountGroupList(List<AbstractJKeyModel> keyList) {
		this.keyList.addAll(keyList);
	}

	@Override
	public boolean add(AbstractJKeyModel e) {
		if (e == null)
			return false;

		return keyList.add(e);
	}

	@Override
	public boolean remove(AbstractJKeyModel e) {
		if (e != null)
			return keyList.remove(e);

		return false;
	}

	@Override
	public void clear() {
		keyList.clear();

	}

	@Override
	public AbstractJKeyModel get(AbstractJKeyModel e) {
		AbstractJKeyModel result = null;
		for (AbstractJKeyModel next : keyList)
			if (next.equals(e))
				return next;
		return result;
	}

	@Override
	public AbstractJKeyModel get(int index) {
		AbstractJKeyModel result = null;
		if (index < keyList.size())
			return keyList.get(index);

		return result;
	}

	@Override
	public List<AbstractJKeyModel> asList() {
		List<AbstractJKeyModel> defensiveCopy = new ArrayList<AbstractJKeyModel>();
		defensiveCopy.addAll(keyList);
		return defensiveCopy;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.tabuto.jkeys.models.Group#getByName(java.lang.String)
	 */
	@Override
	public AbstractJKeyModel getByName(String name) {
		for (AbstractJKeyModel next : this.keyList) {
			if (next.getName().equals(name))
				return next;
		}

		return null;
	}

	@Override
	public String toString() {
		return "AccountGroupList [Name: " + getName() + "]";
	}

}
